package com.zunipe.screen;

import com.zunipe.until.Util;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ScreenTexts;
import net.minecraft.client.gui.screen.option.OptionsScreen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.OptionSliderWidget;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.LiteralText;

public class AutoClickOptionScreen extends Screen
{
    private final Screen parent;

    public AutoClickOptionScreen(Screen parent)
    {
        super(new LiteralText(""));
        this.parent = parent;
    }

    @Override
    protected void init()
    {
        this.addDrawableChild(new ButtonWidget(this.width / 2 - 100, this.height / 6 + 168, 200, 20, ScreenTexts.DONE, button -> this.client.setScreen(this.parent)));
        this.addDrawableChild(new AutoClickOptionScreen.LeftClickIntervalOptions(this.width / 2 - 100, this.height / 6 + 100, 200, 20));
        this.addDrawableChild(new AutoClickOptionScreen.RightClickIntervalOptions(this.width / 2 - 100, this.height / 6 + 75, 200, 20));
    }

    @Override
    public void render(MatrixStack matrices, int mouseX, int mouseY, float delta)
    {
        this.renderBackground(matrices);
        OptionsScreen.drawCenteredText(matrices, this.textRenderer, this.title, this.width / 2, 15, 0xFFFFFF);
        super.render(matrices, mouseX, mouseY, delta);
    }

    private static class RightClickIntervalOptions extends OptionSliderWidget
    {
        public RightClickIntervalOptions(int x, int y, int width, int height)
        {
            super(null, x, y, width, height, (Util.clickRightInterval - 50) / 2950.0);
            this.updateMessage();
        }

        @Override
        public void updateMessage()
        {
            this.setMessage(new LiteralText("右键攻击间隔 " + Util.clickRightInterval + "ms"));
        }

        @Override
        public void applyValue()
        {
            Util.clickRightInterval = (int) (50 + 2950.0 * this.value);
        }
    }

    private static class LeftClickIntervalOptions extends OptionSliderWidget
    {
        public LeftClickIntervalOptions(int x, int y, int width, int height)
        {
            super(null, x, y, width, height, (Util.clickLeftInterval - 50) / 2950.0);
            this.updateMessage();
        }

        @Override
        public void updateMessage()
        {
            this.setMessage(new LiteralText("左键攻击间隔 " + Util.clickLeftInterval + "ms"));
        }

        @Override
        public void applyValue()
        {
            Util.clickLeftInterval = (int) (50 + 2950.0 * this.value);
        }
    }
}
